package com.genesis.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.genesis.mall.common.CustomException;
import com.genesis.mall.common.PmsRecommendProductPage;
import com.genesis.mall.common.PmsRecommendSubjectPage;
import com.genesis.mall.common.Result;
import com.genesis.mall.entity.SmsHomeRecommendProduct;
import com.genesis.mall.entity.SmsHomeRecommendSubject;
import com.genesis.mall.service.ISmsHomeRecommendSubjectService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页推荐专题表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-08-01
 */
@RestController
@RequestMapping("/home/recommendSubject")
@Slf4j
public class SmsHomeRecommendSubjectController {


    @Autowired
    private ISmsHomeRecommendSubjectService smsHomeRecommendSubjectService;
    /**
     * 专题推荐展示
     * @param pageNum
     * @param pageSize
     * @param recommendStatus
     * @return
     */
    @GetMapping("/list")
    public Result<PmsRecommendSubjectPage<SmsHomeRecommendSubject>> list(Long pageNum, Long pageSize, String subjectName, Integer recommendStatus){
        Page<SmsHomeRecommendSubject> pageInfo= new Page(pageNum,pageSize);
        LambdaQueryWrapper<SmsHomeRecommendSubject> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(StringUtils.isNotEmpty(subjectName),SmsHomeRecommendSubject::getSubjectName,subjectName);
        queryWrapper.eq(recommendStatus!=null,SmsHomeRecommendSubject::getRecommendStatus,recommendStatus);
        queryWrapper.orderByDesc(SmsHomeRecommendSubject::getSort);
        smsHomeRecommendSubjectService.page(pageInfo,queryWrapper);

        PmsRecommendSubjectPage<SmsHomeRecommendSubject> productPage = new PmsRecommendSubjectPage<>();
        BeanUtils.copyProperties(pageInfo,productPage);

        return Result.success(productPage);
    }

    /**
     * 修改排序
     * @param sort
     * @param id
     * @return
     */
    @PostMapping("/update/sort/{id}")
    public Result<String>update(@RequestParam String sort , Long id){
        LambdaUpdateWrapper<SmsHomeRecommendSubject> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SmsHomeRecommendSubject::getId,id);
        updateWrapper.set(SmsHomeRecommendSubject::getSort,sort);
       smsHomeRecommendSubjectService.update(updateWrapper);
        return Result.success("修改排序成功");
    }

    /**
     * 专题推荐新增
     * @param list
     * @return
     */
    @PostMapping("/create")
    public Result<String>create(@RequestBody List<SmsHomeRecommendSubject> list){
        for (SmsHomeRecommendSubject smsHomeRecommendSubject : list) {
            LambdaQueryWrapper<SmsHomeRecommendSubject> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SmsHomeRecommendSubject::getSubjectId,smsHomeRecommendSubject.getSubjectId());
            int count = smsHomeRecommendSubjectService.count(wrapper);
            if (count>0){
                throw new CustomException("添加的专题已存在,不可重复添加!");
            }
            smsHomeRecommendSubjectService.save(smsHomeRecommendSubject);
        }
        return Result.success("添加成功");

    }

    /**
     * 专题推荐删除
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public Result<String>delete(@RequestParam List<Integer> ids){
        log.info("ids",ids);

       smsHomeRecommendSubjectService.removeByIds(ids);

        return Result.success("删除成功");
    }

    @PostMapping("/update/recommendStatus")
    public Result<String>updateStatus(@RequestParam List<Integer>ids ,Integer recommendStatus){
        for (Integer id : ids) {
            LambdaUpdateWrapper<SmsHomeRecommendSubject> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(SmsHomeRecommendSubject::getId,id);
            updateWrapper.set(SmsHomeRecommendSubject::getRecommendStatus,recommendStatus);
           smsHomeRecommendSubjectService.update(updateWrapper);
        }
        return Result.success("修改成功");

    }

}
